export default class DoubleQueue<T>{
    private count :number;
    private lowestCount :number;
    private items:Map<number,T>

    constructor(){
        this.count=0;
        this.lowestCount=0;
        this.items=new Map();
    }

    /**
     * @description lowestCount 方向 队头方向入队
     * @param element 入队元素
     */
    addFront(element:T):void{
        this.items.set(--this.lowestCount,element)
    }
    addBack(element:T):void{
        this.items.set(this.count++,element)
    }
    removeFront():T|undefined{
        if(this.isEmpty()){
            return undefined
        }
        const result = this.items.get(this.lowestCount)
        this.items.delete(this.lowestCount++)
        return result
    }
    removeBack():T|undefined{
        if(this.isEmpty()){
            return undefined
        }
        const result = this.items.get(--this.count)
        this.items.delete(this.count)
        return result
    }
    
    isEmpty():boolean{
        return this.items.size==0
    }

    size():number{
        return this.items.size
    }

    clear():void{
        this.items=new Map()
        this.count=0
        this.lowestCount=0
    }

    toString():string{
        if(this.isEmpty()){
            return ''
        }
        let objString:string=`${this.items.get(this.lowestCount)}`
        for(let i=this.lowestCount+1;i<this.count;i++){
            objString=`${objString},${this.items.get(i)}`
        }
        return objString
    }
}